package org.wattdepot.cli.commands;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

/**
 * This class tests the help command of the CommandLineInterface's actual commands.
 * @author David Mau
 */
public class TestCommandHelp {
  String cr = System.getProperty("line.separator");
  
  /**
   * Tests the help command.
   */
  @Test
  public void testHelpCommand() {
    // Create a new help command object.
    HelpCommand command = new HelpCommand( null );
    // Create a new List to store the args
    List<String> args = new ArrayList<String>();
    
    // Get the help output from the command
    String helpOutput = command.doCommand( args );
    
    // Ready the expected output
    String output;
    output = "help";
    output = output.concat(cr);
    output = output.concat("  This message");
    output = output.concat(cr);
    output = output.concat("quit");
    output = output.concat(cr);
    output = output.concat("  Exits the system");
    output = output.concat(cr);
    output = output.concat("server {WattDepotServer}");
    output = output.concat(cr);
    output = output.concat("  Changes the current server to the specified server.");
    output = output.concat(cr);
    output = output.concat("sources");
    output = output.concat(cr);
    output = output.concat("  Prints a table summarizing the public sources in");
    output = output.concat(cr);
    output = output.concat("  the system, one per line.");
    output = output.concat(cr);
    output = output.concat("source {source} summary");
    output = output.concat(cr);
    output = output.concat("  Prints information about a single source.");
    output = output.concat(cr);
    output = output.concat("sensordata {source} timestamp {timestamp}");
    output = output.concat(cr);
    output = output.concat("  Prints information about a single sensor data instance.");
    output = output.concat(cr);
    output = output.concat("dailysensordata {source} day {day}");
    output = output.concat(cr);
    output = output.concat("  Prints a table listing all of the sensor data ");
    output = output.concat(cr);
    output = output.concat("  instances for the specified day.");
    output = output.concat(cr);
    output = output.concat("power {source} timestamp {timestamp}");
    output = output.concat(cr);
    output = output.concat("  Prints the power generated by the source at the given timestamp.");
    output = output.concat(cr);
    output = output.concat("powerstats {source} day {day} sampling-interval ");
    output = output.concat("{minutes} statistic {max|min|average}");
    output = output.concat(cr);
    output = output.concat("  Returns the maximum, minimum, or average power ");
    output = output.concat(cr);
    output = output.concat("  generated by the source for the day.");
    output = output.concat(cr);
    output = output.concat("total [carbon|energy] {source} day {day} ");
    output = output.concat("sampling-interval {minutes}");
    output = output.concat(cr);
    output = output.concat("  Returns the total amount of carbon (or energy) emitted ");
    output = output.concat(cr);
    output = output.concat("  by this source for the given day.");
    output = output.concat(cr);
    output = output.concat("chart power [generated|consumed] {source} {startday} {endday} ");
    output = output.concat("sampling-interval {minutes} file {file}");
    output = output.concat(cr);
    output = output.concat("  Writes out an html file that will display a chart illustrating ");
    output = output.concat(cr);
    output = output.concat("  the power for the source over the interval.");
    output = output.concat(cr);
    output = output.concat("fueltypes {source}");
    output = output.concat(cr);
    output = output.concat("  Returns a table of fuels associated with this source.");
    output = output.concat(cr);
    output = output.concat("  Returns the total power generated by the given fuel type. ");
    output = output.concat(cr);
    output = output.concat("totalpower {source} {timestamp} fueltype {fueltype}");
    output = output.concat(cr);
    output = output.concat("  Generates the total power generated by all non-virtual sources");
    output = output.concat(cr);
    output = output.concat("  associated with this source that use the specified fuel type.");
    output = output.concat(cr);
    output = output.concat("carboncontent {source} {timestamp} sampling-interval {interval}");
    output = output.concat(cr);
    output = output.concat("  Returns the total carbon that would be emitted by that source");
    output = output.concat(cr);
    output = output.concat("  over an hour at the rate occurring as of the timestamp.");
    output = output.concat(cr);
    output = output.concat("energyusage {source} day {start} {end} statistic {max|min}");
    output = output.concat(cr);
    output = output.concat("  Returns the maximum or minimum of power generated by the source");
    output = output.concat(cr);
    output = output.concat("  for the duration.  It checks at the top of the hour and will also");
    output = output.concat(cr);
    output = output.concat("  return when the source hit that maximum or minimum.");
    output = output.concat(cr);
    output = output.concat("energygenerated {source} day {start} {end} statistic {max|min}");
    output = output.concat(cr);
    output = output.concat("  Returns the maximum or minimum of energy generated by the source");
    output = output.concat(cr);
    output = output.concat("  for the duration.  It checks every day started at midnight in the");
    output = output.concat(cr);
    output = output.concat("  duration and samples every 1 hour for that day until midnight of");
    output = output.concat(cr);
    output = output.concat("  the next day.  It will also return the day when the source hit");
    output = output.concat(cr);
    output = output.concat("  that maximum or minimum.");
    output = output.concat(cr);
    output = output.concat("carbonemitted {source} day {start} {end} statistic {max|min}");
    output = output.concat(cr);
    output = output.concat("  Returns the maximum or minimum of carbon emitted by the source");
    output = output.concat(cr);
    output = output.concat("  for the duration.  It checks every day started at midnight in the");
    output = output.concat(cr);
    output = output.concat("  duration and samples every 1 hour for that day until midnight of");
    output = output.concat(cr);
    output = output.concat("  the next day.  It will also return the day when the source hit");
    output = output.concat(cr);
    output = output.concat("  that maximum or minimum.");
    
    // Compare the given output to what is expected
    int result = helpOutput.compareTo(output);
    assertSame("Checking help command", result, 0);
  }
  
  /**
   * Tests to see if the help command can handle excess arguments.
   */
  @Test
  public void testExcessArguments() {
    // Create a new help command object.
    HelpCommand command = new HelpCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking excess Help argument handling", 
        sourceOutput.contains(WattDepotCliCommand.tooManyArguments));
  }
}
